package com.jasper.controller.center;

import com.jasper.controller.BaseController;
import com.jasper.pojo.vo.OrderStatusCountsVO;
import com.jasper.utils.JSONResult;
import com.jasper.utils.PagedGridResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

@Api(value = "用户中心我的订单", tags = {"用户中心我的订单相关接口"})
@RestController
@RequestMapping("myorders")
public class MyOrdersController extends BaseController {

    @ApiOperation(value="查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/query")
    public JSONResult search(
            @ApiParam(name = "userId", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderStatus", value = "订单状态", required = false)
            @RequestParam Integer orderStatus,
            @ApiParam(name = "page", value = "查询下一页的第几页", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页的每一页显示的记录数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)) {
            JSONResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }
        PagedGridResult grid = myOrderService.queryMyOrders(userId, orderStatus, page, pageSize);
        return JSONResult.ok(grid);
    }

    // 商家发货没有后端，这个接口仅仅用于模拟
    @ApiOperation(value="商家发货", notes = "商家发货", httpMethod = "GET")
    @GetMapping("deliver")
    public JSONResult deliver(
            @ApiParam(name = "orderId", value = "订单Id", required = true)
            @RequestParam("orderId")String orderId){

        myOrderService.updateOrderStatus(orderId);

        return JSONResult.ok();
    }

    @ApiOperation(value="用户确认收货", notes = "用户确认收货", httpMethod = "POST")
    @PostMapping("confirmReceive")
    public JSONResult confirmReceive(@RequestParam("orderId")String orderId,@RequestParam("userId")String userId){

        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(!checkResult.isOK()){
            return checkResult;
        }

        boolean res = myOrderService.updateReceiveOrderStatus(orderId);
        if(!res){
            return JSONResult.errorMsg("订单确认收货失败");
        }

        return JSONResult.ok();
    }

    @ApiOperation(value="确认删除", notes = "确认删除", httpMethod = "POST")
    @PostMapping("delete")
    public JSONResult delete(@RequestParam("orderId")String orderId,
                             @RequestParam("userId")String userId){

        JSONResult checkResult = checkUserOrder(userId, orderId);
        if(!checkResult.isOK()){
            return checkResult;
        }
        boolean res = myOrderService.deleteOrder(userId, orderId);
        if(!res){
            return JSONResult.errorMsg("订单删除失败");
        }

        return JSONResult.ok();
    }

    @ApiOperation(value="获得订单状态数概况", notes = "获得订单状态数概况", httpMethod = "POST")
    @PostMapping("statusCounts")
    public JSONResult statusCounts(
            @ApiParam(name = "userId", value = "用户Id", required = true)
            @RequestParam("userId")String userId){
        if(userId==null){
            return JSONResult.errorMsg(null);
        }
        OrderStatusCountsVO myOrderStatusCounts = myOrderService.getMyOrderStatusCounts(userId);
        return JSONResult.ok(myOrderStatusCounts);
    }

    @ApiOperation(value="查询订单动向", notes = "查询订单动向", httpMethod = "POST")
    @PostMapping("trend")
    public JSONResult trend(@RequestParam("userId")String userId,
                            @RequestParam("page")Integer page,
                            @RequestParam("pageSize")Integer pageSize){
        if(userId==null){
            return JSONResult.errorMsg(null);
        }
        page = page==null?1:page;
        pageSize = pageSize==null?10:pageSize;
        PagedGridResult myOrderTrend = myOrderService.getMyOrderTrend(userId, page, pageSize);
        return JSONResult.ok(myOrderTrend);
    }

}
